var app = getApp()
Page({
  data: {
    needBindMobile: true,
    useCodeToLogIn: true,
    mobile: '',
    userInfo: { //用户信息
      avatarUrl: '',
      nickName: ''
    },
    codevalue:'',
    zichan_slides: [],
    baye_rank: null,
    disableGetMobileCode: false,
    disableSubmitMobileCode: false,
    getCodeButtonText: '获取验证码'
  },
  onLoad: function () {
       var that = this
      wx.checkSession({
        success: function (res) {
          console.log(res)
          var session_3rd = wx.getStorageSync('session_3rd')
          //登录态未过期
            wx.request({
              url: `${app.globalData.DOMAIN}/mpbase/wxapp/checkSession`,
              data: {
                session_3rd: session_3rd
              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              // header: {}, // 设置请求的 header
              success: function (res) {
                console.log(res)
                
              },
              fail: function () {
                // fail
              },
              complete: function () {
                // complete
              }
            })
          

        },
        fail: function (res) {
          //登录态过期
          this.loginUct();
        }
      })
    var that = this
    that.setData({ userInfo: app.globalData.userInfo })
    if (app.globalData.token) {
      profile.postCustomerInfo({}, that.infoCallback)
    } else {
      var token = wx.getStorageSync('userToken')
      if (token) {
        app.globalData.token = token
        profile.postCustomerInfo({}, that.infoCallback)
      }
    }

  },

  bindGetPassCode: function (e) { //点击获取验证码的方法
    var that = this
    wx.request({
      url: `${app.globalData.API_URL}/verify/sendVerify`,
      data: {
        mobile:e.detail.value.mobile
      },
      method: 'post',
      header: {
        "content-type":"application/x-www-form-urlencoded"
      }, // 设置请求的 header
      success: function(res){
        console.log(res)
        wx.showToast({
          title: '短信发送成功',
          icon: 'success',
          duration: 2000
        })
        that.countDownPassCode()
      },
      fail: function(res) {
        console.log(res)
      },
      complete: function() {
       
      }
    })
  },

  countDownPassCode: function () {
    var pages = getCurrentPages()
    var i = 60
    var intervalId = setInterval(function () {
      i--
      if (i <= 0) {
        pages[pages.length - 1].setData({
          disableGetMobileCode: false,
          disableSubmitMobileCode: false,
          getCodeButtonText: '获取验证码'
        })
        clearInterval(intervalId)
      } else {
        pages[pages.length - 1].setData({
          getCodeButtonText: i,
          disableGetMobileCode: true,
          disableSubmitMobileCode: false
        })
      }
    }, 1000);
  },
  /**
   * 验证手机号
   */
  bindCheckMobile: function (mobile) {
    if (!mobile.match(/^1[3-9][0-9]\d{8}$/)) {
      wx.showModal({
        title: '错误',
        content: '手机号格式不正确，仅支持国内手机号码'
      })
      return false
    }
    return true
  },
  /**
   * 获取手机号
   */
  bindInputMobile: function (e) {
    this.setData({
      mobile: e.detail.value,
    })
  },
  GetMobileCode:function(){
   
  },
  /**
   * 登录中
   */
  bindLoginMobilecode: function (e) {  //点击提交时的方法
    var that=this
    console.log(e.detail.value.code)
    console.log(e.detail.value.mobile)
    wx.request({
      url:`${app.globalData.API_URL}/verify/checkVerify`,
      data: {
        mobile:e.detail.value.mobile,
        verify:e.detail.value.code
      },
      method: 'post',
       header: {
        "content-type":"application/x-www-form-urlencoded"
      }, // 设置请求的 header
      success: function(res){
        console.log(res)
        if(res.data==1){
          wx.navigateTo({
            url: '../successInfo/successInfo',
          })
        }else{
           wx.showToast({
          title: '验证码错误，请重新发送验证！',
          icon: 'fail',
          duration: 2000
        })
        }
        that.setData({
          codevalue:[]
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 登录中
   */
  bindLoginPassword: function (e) {
    var mobile = e.detail.value.mobile
    if (!this.bindCheckMobile(mobile)) { return }
    wx.showToast({
      title: '登录中...',
      icon: 'loading',
      duration: 5000
    })
    var data = { mobile: mobile, password: e.detail.value.password }
    profile.postCustomerInfo(data, this.infoCallback)
  },
  /**
   * 
   */
  changeLoginType: function (e) {
    this.setData({ useCodeToLogIn: !this.data.useCodeToLogIn })
  },
  /**
   * 
   */
  bindLogout: function (e) {
    var that = this
    app.request({
      url: `${app.globalData.API_URL}/sessions/logout`,
      data: { code: app.globalData.code },
      method: 'POST',
      success: function (res) {
        if (parseInt(res.statusCode) === 200) {
          wx.removeStorage({ key: 'token' })
          app.globalData.currentCustomer = null
          app.globalData.token = null
          app.globalData.code = null
          app.globalData.userInfo = null
          app.getUserInfo(function (userInfo) {
            that.setData({ userInfo: userInfo })
          })
          that.setData({
            needBindMobile: true,
            baye_rank: ''
          })
        }
      },
      fail: function () { },
      complete: function () { }
    })
  },

})
